<template>
    <div class="app-container">
        <div v-if="$route.path == '/accountManagement/roleList'">
            <section>
                <div class="content" v-loading="loading">
                    <el-row class="top" :gutter="10" type="flex" align="middle">
                        <el-col :span="3" :md="5" :lg="3">
                            <el-button type="primary" size="mini" @click="$router.push(`/accountManagement/roleList/roleDetail?title=添加`)">添加</el-button>
                            <el-button @click="del" size="mini" type="danger">删除</el-button>
                        </el-col>
                        <!-- <el-col :span="4">
                            <el-input v-model="form.keys" placeholder="名称/账号/电话/负责人" style="width:100%" suffix-icon="el-icon-search" @blur="getCompanyList(1)"></el-input>
                        </el-col> -->
                    </el-row>
                    <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%;margin-top:10px;" :header-cell-style="{background:'#f5f6f8'}">
                        <el-table-column type="selection" width="55">
                        </el-table-column>
                        <el-table-column prop="id" label="编号">
                        </el-table-column>
                        <el-table-column prop="name" label="角色">
                        </el-table-column>
                        <el-table-column label="操作" width="330">
                            <template slot-scope="scope">
                                <el-button @click="handleClick(scope.row,0)" size="mini">编辑</el-button>
                                <!-- <el-button @click="handleClick(scope.row,1)" type="info" size="mini">详情</el-button> -->
                                <el-button @click="handleClick(scope.row,1)" type="danger" size="mini">删除</el-button>
                                <el-button @click="handleClick(scope.row,2)" type="primary" size="mini">权限分配</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination style="margin-top: 40px;text-align: center;padding-bottom: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="form.page" :page-sizes="[10, 20, 50, 100]" :page-size="form.limit" layout="total, sizes, prev, pager, next, jumper" :total="form.total">
                    </el-pagination>
                </div>
            </section>
        </div>
        <router-view></router-view>
    </div>
</template>

<script>

export default {
    data() {
        return {
            form: {
                keys: '',
                total: 0,
                limit: 10,
                page: 1,
            },
            id: '',
            tableData: [],
            loading: false,
        }
    },
    created() {

    },
    mounted() {
        this.getCompanyList()
    },
    methods: {
        // 获取公司列表
        async getCompanyList(page) {
            if (page) {
                this.form.page = page
            }
            this.loading = true
            let res = await this.$Http.roleList(this.form)
            if (res.status == 1) {
                this.tableData = res.data.data || []
                this.form.total = res.data.total
            }
            this.loading = false
        },
        del() {
            let data = this.$refs.multipleTable.selection;
            if (!data.length) {
                this.$message.warning("请至少选择一个选项");
                return false;
            }
            let ids = [];
            for (let item of data) {
                ids.push(item.id);
            }
            this.$confirm("选中的选项将会被删除, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(async () => {
                let res = await this.$Http.delRole({ id: ids.join(",") })
                if (res.status == 1) {
                    if (ids.length == this.tableData.length) {
                        this.form.page -= 1
                        this.form.page = this.form.page < 1 ? 1 : this.form.page

                    }
                    this.$message.success('删除成功');
                    await this.getCompanyList()
                }

            })
        },
        handleClick(row, flag) {
            if (flag == 0) {
                //编辑
                this.$router.push(`/accountManagement/roleList/roleDetail?title=编辑&id=${row.id}`)
            } else if (flag == 1) {
                //详情
                this.$confirm("该条将会被删除, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }).then(async () => {
                    let res = await this.$Http.delRole({ id: row.id })
                    if (res.status == 1) {
                        if (this.tableData.length == 1) {
                            this.form.page -= 1
                            this.form.page = this.form.page < 1 ? 1 : this.form.page
                        }
                        this.$message.success('删除成功');
                        await this.getCompanyList()
                    }
                })
            } else {
                this.$router.push(`/accountManagement/roleList/rootDdistribution?id=${row.id}&name=${row.name}`)
            }
        },
        handleSizeChange(val) {
            this.form.limit = val
            this.getCompanyList()
        },
        handleCurrentChange(val) {
            this.form.page = val
            this.getCompanyList()
        }
    }
}
</script>

<style lang="scss" scoped>
.app-container {
    .kong {
        height: 40px;
    }
    section {
        background: #eee;
        padding: 30px;
        .content {
            width: 100%;
            height: 100%;
            background: #fff;
            border-radius: 16px;
            padding-top: 18px;
            .top {
                padding-left: 29px;
            }
            ::v-deep .el-checkbox__input {
                margin-left: 10px;
            }
            ::v-deep .el-date-editor {
                width: -webkit-fill-available;
            }
        }
    }
}
</style>